package com.example.im.controller.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.Nullable;

import com.example.im.R;
import com.example.im.model.Model;
import com.example.im.model.bean.UserInfo;
import com.hyphenate.chat.EMClient;

public class SplashActivity extends Activity {
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            //如果当前activity已经推出，那么我就不处理hander中的消息
            if(isFinishing()){
                return;
            }

            //判断进入主页面还是登录页面
            toMainOrLogin();
        }
    };
    //判断进入主页面还是登录页面
    private void toMainOrLogin() {
//        new Thread() {
//            public void run() {
//
//            }
//        }.start();

        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //判断当前账号是否已经登陆过(环信给的判断接口)
                if (EMClient.getInstance().isLoggedInBefore()) {//登录过

                    //获取到当前登录用户的信息
                    UserInfo accountByHxId = Model.getInstance().getUerAccountDao().getAccountByHxId(EMClient.getInstance().getCurrentUser());

                    if (accountByHxId == null){
                        //跳转到登录界面
                        Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }else {
                        //登录成功后的方法
                        Model.getInstance().loginSuccess(accountByHxId);

                        //跳转页面
                        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                        startActivity(intent);
                    }

                } else {//没登陆过
                    //跳转到登录页面
                    Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                    startActivity(intent);
                }

                //结束当前页面
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁消息
        handler.removeCallbacksAndMessages(null);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        //发送2s钟的延时消息
        handler.sendMessageDelayed(Message.obtain(),2000);



    }
}
